package com.qa.controller;

import com.qa.common.Const;
import com.qa.pojo.Tag;
import com.qa.pojo.User;
import com.qa.service.ITagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author
 * @Date 2020/3/7 22:32
 * @Desc
 **/
@Controller
public class TagController {

    @Autowired
    private ITagService tagService;

    @GetMapping("/user/tags")
    public String test(HttpServletRequest request, Model model) {
        User user = (User) request.getSession().getAttribute(Const.CURRENT_USER);
        List<Tag> tags = tagService.getByEmail(user.getEmail());
        model.addAttribute("tags", tags);
        return "myTags";
    }


    @GetMapping("/tag/add")
    public String tagAdd(@RequestParam("tagName") String tagName, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute(Const.CURRENT_USER);
        tagService.add(user.getEmail(), tagName);
        return "redirect:/user/tags";
    }


    @GetMapping("/tag/delete")
    public String deleteTag(@RequestParam("tagId") Integer tagId){
        tagService.delete(tagId);
        return "redirect:/user/tags";
    }


}
